package cn.huhaoran.yaoyaolingxian.ui;

import cn.huhaoran.yaoyaolingxian.util.YaoYaoLingXianUtils;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import java.util.List;

/**
 * @author huhaoran
 * @description:
 * @date 2024/7/14
 */
public class NewPageAndeViewModelContainer {
    private JPanel panel1;
    private JPanel mainPanel;
    public TextFieldWithBrowseButton jsonPath;
    public TextFieldWithBrowseButton enumPath;
    public JComboBox<String> enumName;
    public JCheckBox useV2CheckBox;
    public JCheckBox onWillAppearCheckBox;
    public JCheckBox onAppearCheckBox;
    public JCheckBox onWillShowCheckBox;
    public JCheckBox onShownCheckBox;
    public JCheckBox onWillHideCheckBox;
    public JCheckBox onHiddenCheckBox;
    public JCheckBox onWillDisappearCheckBox;
    public JCheckBox onDisappearCheckBox;
    public Map<String, List<String>> enumNameMap;

    public Map<String, List<String>> getEnumNameMap() {
        return enumNameMap;
    }

    public JComboBox getEnumName() {
        // 添加双击监听器
        enumName.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    enumName.setEditable(true);
                    enumName.getEditor().getEditorComponent().requestFocus();
                }
            }
        });
        // 添加 ActionListener 来监听编辑完成事件
/*        enumName.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });*/
        // 添加回车事件
        enumName.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    // 处理回车键事件，例如验证输入或触发特定操作
                    // 获取编辑后的文本
                    String newItem = enumName.getEditor().getItem().toString();
                    // 检查文本是否为空或已存在
                    ComboBoxModel<String> model = enumName.getModel();
                    int size = model.getSize();
                    if(size == 0){
                        // 空的 添加新的项到 JComboBox
                        enumName.addItem(newItem);
                        enumName.setEditable(false);
                    }else {
                        for(int i = 0; i < size; i++){
                            if(model.getElementAt(i).equals(newItem)){
                                YaoYaoLingXianUtils.showMessgerOnBalloon(enumName, newItem+" 已经存在！");
                                return;
                            }
                        }
                    }
                    e.consume();  // 阻止事件进一步传播 目前失效
                }
            }
        });
        return enumName;
    }

    public TextFieldWithBrowseButton getJsonPath() {
        jsonPath.addBrowseFolderListener("选择JSON路由文件", "", null, new FileChooserDescriptor(true, false, false, false, true, false));
        return jsonPath;
    }

    public TextFieldWithBrowseButton getEnumPath() {
        // 添加选择枚举文件的监听事件
        FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(true, false, false, false, true, false);
        fileChooserDescriptor.setTitle("选择枚举配置文件");
        enumPath.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor) {
            @Override
            public void onFileChosen(@NotNull VirtualFile chosenFile) {
                String selectedFilePath = chosenFile.getPath();
                enumPath.setText(selectedFilePath);
                // 在这里添加选择文件后的处理逻辑
                System.out.println("Selected file: " + selectedFilePath);
                //String selectedFilePath = enumPath.getText(); // 获取选择的文件路径
                // 执行额外的操作，例如打印路径
                System.out.println("Selected file path: " + selectedFilePath);
                // 用psijava解析文件，获取该文件内所有枚举类的名字
                VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(selectedFilePath);
                String filePath = virtualFile.getPath();
                System.out.println(filePath);
                try {
                    enumNameMap = YaoYaoLingXianUtils.parseFileContent(filePath);
                } catch (IOException e) {
                    YaoYaoLingXianUtils.showMessgerOnBalloon(enumPath, " 解析文件时发生错误！："+e.getMessage());
                }
                // 打印结果
                    enumNameMap.forEach((key, value) -> {
                        System.out.println("Enum Name: " + key);
                        System.out.println("Values: " + value);
                    });
                enumNameMap.keySet().forEach((item-> enumName.addItem(item)));
            }
        });
        return enumPath;
    }



    public JPanel getMainPanel() {

        return mainPanel;
    }

}
